package com.ocom.payhandle.config.loadbalancer;

import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * 策略 取最大版本号服务
 */

@RefreshScope
public class MyRandomRule extends AbstractLoadBalancerRule {


    @Override
    public Server choose(Object key) {

        ILoadBalancer loadBalancer = getLoadBalancer();
        if (loadBalancer == null) {
            return null;
        }
        List<Server> allServers = loadBalancer.getAllServers();
        if (allServers == null || allServers.size() == 0) {
            return null;
        }

        //最大版本
        List<NacosServer> maxVerServers = new ArrayList<>();
        //其他服务list
        List<NacosServer> nacosServers = new ArrayList<>();

        if (allServers.size() == 1) {
            Server server = allServers.get(0);
            return server;
        }

        //最大版本
        String versionMax = null;
        for (Server server1 : allServers) {
            //灰度策略
            NacosServer server2 = (NacosServer) server1;
            Map<String, String> metadata = server2.getMetadata();
            String metadataVersion = metadata.get("version");
            if(metadataVersion==null){
                metadataVersion = "1.0.1" ;
            }

            if (versionMax == null) {
                versionMax = metadataVersion;
            }

            Integer metadataVersiona = 1;
            Integer versionMaxb = 1;
            try {
                metadataVersiona = Integer.valueOf(metadataVersion.substring(4));
                versionMaxb = Integer.valueOf(versionMax.substring(4));
            } catch (Exception e) {

            }
            if (metadataVersiona.compareTo(versionMaxb) > 0) {
                versionMax = metadataVersion;
            }

        }

        //返回最大版本
        for (Server server1 : allServers) {
            //灰度策略
            NacosServer server2 = (NacosServer) server1;
            Map<String, String> metadata = server2.getMetadata();
            String metadataVersion = metadata.get("version");
            if(metadataVersion==null){
                metadataVersion = "1.0.1" ;
            }
            if (versionMax.equals(metadataVersion)) {
                maxVerServers.add(server2);
            } else {
                nacosServers.add(server2);
            }
        }
        if (maxVerServers.size() > 0) {
            int size = maxVerServers.size();
            Random random = new Random();
            return maxVerServers.get(random.nextInt(size));
        } else {
            int size = nacosServers.size();
            Random random = new Random();
            return nacosServers.get(random.nextInt(size));
        }
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {

    }
}
